<?session_start();?>
<? if(true)
{

	/*if(strstr($_SERVER['REQUEST_URI'],"admin/") && !strstr($_SERVER['REQUEST_URI'],"verify"))
	{
		if($_SESSION['inlog'] >= 1) {}
		else exit("<script type=\"text/javascript\">window.location='?body=login';</script>");
	}
*/

	////////////////////////////////////////////////// class ///////////
	class pagination
	{
		public function __construct()
		{
		}
		public function cp($total_rows, $rows_per_page, $page_num , $param)
		{
			$arr = array();
			// calculate last page
			$last_page = ceil($total_rows / $rows_per_page);
			// make sure we are within limits
			$page_num = (int) $page_num;
			if ($page_num < 1)
			{
			   $page_num = 1;
			} 
			elseif ($page_num > $last_page)
			{
			   $page_num = $last_page;
			}
			$upto = ($page_num - 1) * $rows_per_page;
			$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
			$arr['current'] = $page_num;
			if ($page_num == 1)
				$arr['previous'] = $page_num;
			else
				$arr['previous'] = $page_num - 1;
			if ($page_num == $last_page)
				$arr['next'] = $last_page;
			else
				$arr['next'] = $page_num + 1;
			$arr['last'] = $last_page;
			$arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
			$arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
			$arr['param'] = $param;
			return $arr;
		}
		function get_surrounding_pages($page_num, $last_page, $next)
		{
			$arr = array();
			$show = 4; // how many boxes
			// at first
			if ($page_num == 1)
			{
				// case of 1 page only
				if ($next == $page_num) return array(1);
				for ($i = 0; $i < $show; $i++)
				{
					if ($i == $last_page) break;
					array_push($arr, $i + 1);
				}
				return $arr;
			}
			// at last
			if ($page_num == $last_page)
			{
				$start = $last_page - $show;
				if ($start < 1) $start = 0;
				for ($i = $start; $i < $last_page; $i++)
				{
					array_push($arr, $i + 1);
				}
				return $arr;
			}
			// at middle
			$start = $page_num - $show;
			if ($start < 1) $start = 0;
			for ($i = $start; $i < $page_num; $i++)
			{
				array_push($arr, $i + 1);
			}
			for ($i = ($page_num + 1); $i < ($page_num + $show); $i++)
			{
				if ($i == ($last_page + 1)) break;
				array_push($arr, $i);
			}
			return $arr;
		}
		
	}
	////////////////////////////////////////////////end pagination class 				
							
	function shownav($url,$parr,$mode,$search)
	{
		if($parr['current'] > 1){
			if($mode == "js"){
				$prev_link = "onclick=\"showdiv2('class','search_panel','ajax_showplist.php?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$prev_link = 'href=\''.$url."?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			
			$btn_prev = " <img src = './images/nav-prev.png' />";
			$btn_prev_over = "./images/nav-prev-over.png";
			$btn_prev_out = "./images/nav-prev.png";
		}else{
			$prev_link = "#";
			$btn_prev = " <img src = './images/nav-prev-dark.png' />";
			$btn_prev_over = "./images/nav-prev-over-dark.png";
			$btn_prev_out = "./images/nav-prev-dark.png";
		}
		
		if($parr['current'] >= $parr['last']){
			$next_link = "#";
			$btn_next = " <img src = './images/nav-next-dark.png' />";
			$btn_next_over = "./images/nav-next-over-dark.png";
			$btn_next_out = "./images/nav-next-dark.png";
		}else{
			if($mode == "js"){
				$next_link = "onclick=\"showdiv2('class','search_panel','ajax_showplist.php?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$next_link = 'href=\''.$url."?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			$btn_next = " <img src = './images/nav-next.png' />";
			$btn_next_over = "./images/nav-next-over.png";
			$btn_next_out = "./images/nav-next.png";
		}
		
		
		//$a .= "<a href='".$url."?page=1&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> << </a>";
		$a .= "<a ".$prev_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_prev_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_prev_out."'})\"
		> ".$btn_prev." </a>";
		
		
		foreach($parr['pages'] as $pa)
		{
			if($pa == $parr['current']) $br = array("<b>","</b>");
			else $br = array();
			//$a .= "<a href='".$url."?page=".$pa."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> ".$br[0].$pa.$br[1]." </a>";
		}
		
		
		$a .= "<a ".$next_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_next_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_next_out."'})\"
			> ".$btn_next." </a>";
		//$a .= "<a href='".$url."?page=".$parr['last']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> >> </a>";		
		return $a;					
	}
	
	
	function showpromnav($url,$parr,$mode,$search)
	{
		if($parr['current'] > 1){
			if($mode == "js"){
				$prev_link = "onclick=\"showdiv2('class','promolist','ajax_showpromlist.php?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$prev_link = 'href=\''.$url."?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			
			$btn_prev = " <img src = './images/nav-prev.png' />";
			$btn_prev_over = "./images/nav-prev-over.png";
			$btn_prev_out = "./images/nav-prev.png";
		}else{
			$prev_link = "#";
			$btn_prev = " <img src = './images/nav-prev-dark.png' />";
			$btn_prev_over = "./images/nav-prev-over-dark.png";
			$btn_prev_out = "./images/nav-prev-dark.png";
		}
		
		if($parr['current'] >= $parr['last']){
			$next_link = "#";
			$btn_next = " <img src = './images/nav-next-dark.png' />";
			$btn_next_over = "./images/nav-next-over-dark.png";
			$btn_next_out = "./images/nav-next-dark.png";
		}else{
			if($mode == "js"){
				$next_link = "onclick=\"showdiv2('class','promolist','ajax_showpromlist.php?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$next_link = 'href=\''.$url."?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			$btn_next = " <img src = './images/nav-next.png' />";
			$btn_next_over = "./images/nav-next-over.png";
			$btn_next_out = "./images/nav-next.png";
		}
		
		
		//$a .= "<a href='".$url."?page=1&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> << </a>";
		$a .= "<a ".$prev_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_prev_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_prev_out."'})\"
		> ".$btn_prev." </a>";
		
		
		foreach($parr['pages'] as $pa)
		{
			if($pa == $parr['current']) $br = array("<b>","</b>");
			else $br = array();
			//$a .= "<a href='".$url."?page=".$pa."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> ".$br[0].$pa.$br[1]." </a>";
		}
		
		
		$a .= "<a ".$next_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_next_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_next_out."'})\"
			> ".$btn_next." </a>";
		//$a .= "<a href='".$url."?page=".$parr['last']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> >> </a>";		
		return $a;					
	}
	
	function showpublishnav($url,$parr,$mode,$search)
	{
		if($parr['current'] > 1){
			if($mode == "js"){
				$prev_link = "onclick=\"showdiv2('class','publishlist','ajax_showpublishlist.php?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$prev_link = 'href=\''.$url."?page=".$parr['previous']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			
			$btn_prev = " <img src = './images/nav-prev.png' />";
			$btn_prev_over = "./images/nav-prev-over.png";
			$btn_prev_out = "./images/nav-prev.png";
		}else{
			$prev_link = "#";
			$btn_prev = " <img src = './images/nav-prev-dark.png' />";
			$btn_prev_over = "./images/nav-prev-over-dark.png";
			$btn_prev_out = "./images/nav-prev-dark.png";
		}
		
		if($parr['current'] >= $parr['last']){
			$next_link = "#";
			$btn_next = " <img src = './images/nav-next-dark.png' />";
			$btn_next_over = "./images/nav-next-over-dark.png";
			$btn_next_out = "./images/nav-next-dark.png";
		}else{
			if($mode == "js"){
				$next_link = "onclick=\"showdiv2('class','publishlist','ajax_showpublishlist.php?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."&aum_txtSearch1=".$search."');\"";
			}else{
				$next_link = 'href=\''.$url."?page=".$parr['next']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."\'";
			}
			$btn_next = " <img src = './images/nav-next.png' />";
			$btn_next_over = "./images/nav-next-over.png";
			$btn_next_out = "./images/nav-next.png";
		}
		
		
		//$a .= "<a href='".$url."?page=1&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> << </a>";
		$a .= "<a ".$prev_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_prev_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_prev_out."'})\"
		> ".$btn_prev." </a>";
		
		
		foreach($parr['pages'] as $pa)
		{
			if($pa == $parr['current']) $br = array("<b>","</b>");
			else $br = array();
			//$a .= "<a href='".$url."?page=".$pa."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> ".$br[0].$pa.$br[1]." </a>";
		}
		
		
		$a .= "<a ".$next_link." onmouseover=\"$(this).children('img').attr({'src' : '".$btn_next_over."'})\"
			onmouseout=\"$(this).children('img').attr({'src' : '".$btn_next_out."'})\"
			> ".$btn_next." </a>";
		//$a .= "<a href='".$url."?page=".$parr['last']."&".$parr['param'][0][0]."=".$parr['param'][0][1]."'> >> </a>";		
		return $a;					
	}					
							
							

	if($_REQUEST['lang'] == "en") $_SESSION['lang'] = "en";
	else if($_SESSION['lang'] == "" || $_SESSION['lang'] == "th" || $_REQUEST['lang'] == "th") $_SESSION['lang'] = "th";
	else $_SESSION['lang'] = "en";
	}
	if($_REQUEST['p'] != "") $_SESSION['p'] = $_REQUEST['p'];
	include("dbpasswordonly.php");




	function imgResizeSave($paths,$images,$height,$tmp_name)
	{
		if($tmp_name != "") { //เช็คว่ามีการอัปรูป
		copy($tmp_name, $paths.$images); //ทำการ copy รูป
		
		
		$size = getimagesize($paths.$images);
		$width = round($height*$size[0]/$size[1]); //ขนาด ความกว้่างคำนวนเพื่อความสมส่วนของรูป
		if($size[2] == 1) {
		$images_orig = imagecreatefromgif($paths.$images); //resize รูปประเภท GIF
		} else if($size[2] == 2) {
		$images_orig = imagecreatefromjpeg($paths.$images); //resize รูปประเภท JPEG
		}
		$photoX = imagesx($images_orig);
		$photoY = imagesy($images_orig);
		$images_fin = imagecreatetruecolor($width, $height);
		imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		imagejpeg($images_fin, $paths.str_replace(".","-swap.",$images)); //ชื่อไฟล์ใหม่
		//imagejpeg($images_fin, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		
		// Gray 
		$im = ImageCreateFromJpeg($paths.str_replace(".","-swap.",$images));
				$imgw = imagesx($im);
				$imgh = imagesy($im);
				
				for ($i=0; $i<$imgw; $i++)
				{
						for ($j=0; $j<$imgh; $j++)
						{
					   
								// get the rgb value for current pixel
							   
								$rgb = ImageColorAt($im, $i, $j);
							   
								// extract each value for r, g, b
							   
								$rr = ($rgb >> 16) & 0xFF;
								$gg = ($rgb >> 8) & 0xFF;
								$bb = $rgb & 0xFF;
							   
								// get the Value from the RGB value
							   
								$g = round(($rr + $gg + $bb) / 3);
							   
								// grayscale values have r=g=b=g
							   
								$val = imagecolorallocate($im, $g, $g, $g);
							   
								// set the gray value
							   
								imagesetpixel ($im, $i, $j, $val);
						}
				}
				imagejpeg($im, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		// Gray 
		
		imagedestroy($im);
		imagedestroy($images_orig);
		imagedestroy($images_fin);
		} 	
	
	
	}

	function imgResizeSave2($paths,$images,$width,$height,$tmp_name)
	{	//echo $width."  ".$height;exit;
		//echo $images."<br/>"; 
		if($tmp_name != "") { //เช็คว่ามีการอัปรูป
		copy($tmp_name, $paths.$images); //ทำการ copy รูป
		
		$size = getimagesize($paths.$images);
		
		// find  width or height is higher?
		//echo "width =>  height => ";
		if($size[0] < $size[1]){//height higher
			if($size[1] > $height && $height > 0){
				
				$height1 = $height;
				$chk = true;
				while($chk){
					$ratio = ($size[1]/$height1);
					$width1 = $size[0]/$ratio;
					if($width1 > $width){
						$height1 -= 50;
					}else{
						$chk = false;
					}
				}
				
				$width = $size[0]/$ratio;
				$height = $size[1]/$ratio;
			}else{
				$width = $size[0];
				$height = $size[1];
			}
		}else{//width higher
		
			if($size[0] > $width && $width > 0){
				$width1 = $width;
				$chk = true;
				while($chk){
					$ratio = ($size[0]/$width1);
					$height1 = $size[1]/$ratio;
					if($height1 > $height){
						$width1 -= 50;
					}else{
						$chk = false;
					}
				}
				$height = $size[1]/$ratio;
				$width = $size[0]/$ratio;
			}else{
				$width = $size[0];
				$height = $size[1];
			}
		}
		
		
		//$width = round($height*$size[0]/$size[1]); //ขนาด ความกว้่างคำนวนเพื่อความสมส่วนของรูป
		if($size[2] == 1) {
		$images_orig = imagecreatefromgif($paths.$images); //resize รูปประเภท GIF
		} else if($size[2] == 2) {
		$images_orig = imagecreatefromjpeg($paths.$images); //resize รูปประเภท JPEG
		}
		$photoX = imagesx($images_orig);
		$photoY = imagesy($images_orig);
		$images_fin = imagecreatetruecolor($width, $height);
		imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		imagejpeg($images_fin, $paths.str_replace(".","-swap.",$images)); //ชื่อไฟล์ใหม่
		copy($paths.str_replace(".","-swap.",$images), $paths.$images); //ชื่อไฟล์ใหม่
		//imagejpeg($images_fin, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		
		// Gray 
		$im = ImageCreateFromJpeg($paths.str_replace(".","-swap.",$images));
				$imgw = imagesx($im);
				$imgh = imagesy($im);
				
				for ($i=0; $i<$imgw; $i++)
				{
						for ($j=0; $j<$imgh; $j++)
						{
					   
								// get the rgb value for current pixel
							   
								$rgb = ImageColorAt($im, $i, $j);
							   
								// extract each value for r, g, b
							   
								$rr = ($rgb >> 16) & 0xFF;
								$gg = ($rgb >> 8) & 0xFF;
								$bb = $rgb & 0xFF;
							   
								// get the Value from the RGB value
							   
								$g = round(($rr + $gg + $bb) / 3);
							   
								// grayscale values have r=g=b=g
							   
								$val = imagecolorallocate($im, $g, $g, $g);
							   
								// set the gray value
							   
								imagesetpixel ($im, $i, $j, $val);
						}
				}
				imagejpeg($im, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		// Gray 
		
		imagedestroy($im);
		imagedestroy($images_orig);
		imagedestroy($images_fin);
		} 	
	
	
	}
	
	function imgResizeSave3($paths,$images,$longedge,$shotdge,$tmp_name)//สูตรนี้รูปภาพจะยาวไปตามด้านที่ยาวที่สุดแต่กำหนดให้ไม่เกิน longedge
	{	//echo $width."  ".$height;exit;
		//echo $images."<br/>"; 
		if($tmp_name != "") { //เช็คว่ามีการอัปรูป
		copy($tmp_name, $paths.$images); //ทำการ copy รูป
		
		$size = getimagesize($paths.$images);
		
		// find  width or height is higher?
		//echo "width =>  height => ";
		if($size[0] < $size[1]){//height higher
			$height = $longedge;
			$width = $shotdge;
			if($size[1] > $height && $height > 0){
				
				$height1 = $height;
				$chk = true;
				while($chk){
					$ratio = ($size[1]/$height1);
					$width1 = $size[0]/$ratio;
					if($width1 > $width){
						$height1 -= 50;
					}else{
						$chk = false;
					}
				}
				
				$width = $size[0]/$ratio;
				$height = $size[1]/$ratio;
			}else{
				$width = $size[0];
				$height = $size[1];
			}
		}else{//width higher
			$width = $longedge;
			$height = $shotdge;
			if($size[0] > $width && $width > 0){
				$width1 = $width;
				$chk = true;
				while($chk){
					$ratio = ($size[0]/$width1);
					$height1 = $size[1]/$ratio;
					if($height1 > $height){
						$width1 -= 50;
					}else{
						$chk = false;
					}
				}
				$height = $size[1]/$ratio;
				$width = $size[0]/$ratio;
			}else{
				$width = $size[0];
				$height = $size[1];
			}
		}
		
		
		//$width = round($height*$size[0]/$size[1]); //ขนาด ความกว้่างคำนวนเพื่อความสมส่วนของรูป
		if($size[2] == 1) {
		$images_orig = imagecreatefromgif($paths.$images); //resize รูปประเภท GIF
		} else if($size[2] == 2) {
		$images_orig = imagecreatefromjpeg($paths.$images); //resize รูปประเภท JPEG
		}
		$photoX = imagesx($images_orig);
		$photoY = imagesy($images_orig);
		$images_fin = imagecreatetruecolor($width, $height);
		imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		imagejpeg($images_fin, $paths.str_replace(".","-swap.",$images)); //ชื่อไฟล์ใหม่
		copy($paths.str_replace(".","-swap.",$images), $paths.$images); //ชื่อไฟล์ใหม่
		//imagejpeg($images_fin, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		
		// Gray 
		$im = ImageCreateFromJpeg($paths.str_replace(".","-swap.",$images));
				$imgw = imagesx($im);
				$imgh = imagesy($im);
				
				for ($i=0; $i<$imgw; $i++)
				{
						for ($j=0; $j<$imgh; $j++)
						{
					   
								// get the rgb value for current pixel
							   
								$rgb = ImageColorAt($im, $i, $j);
							   
								// extract each value for r, g, b
							   
								$rr = ($rgb >> 16) & 0xFF;
								$gg = ($rgb >> 8) & 0xFF;
								$bb = $rgb & 0xFF;
							   
								// get the Value from the RGB value
							   
								$g = round(($rr + $gg + $bb) / 3);
							   
								// grayscale values have r=g=b=g
							   
								$val = imagecolorallocate($im, $g, $g, $g);
							   
								// set the gray value
							   
								imagesetpixel ($im, $i, $j, $val);
						}
				}
				imagejpeg($im, $paths.str_replace(".","-tmp.",$images)); //ชื่อไฟล์ใหม่
		// Gray 
		
		imagedestroy($im);
		imagedestroy($images_orig);
		imagedestroy($images_fin);
		} 	
	
	
	}

	function btswap()
	{	
		
		$a = " onmouseover=\"javascript:if(this.src.indexOf('-active')>=0)return false;var apic=this.src.split('-swap').join('');var picswap=apic.split('.gif').join('-swap.gif');picswap=picswap.split('.jpg').join('-swap.jpg');picswap=picswap.split('.png').join('-swap.png');this.src=picswap;\" onmouseout=\"javascript:if(this.src.indexOf('-active')>=0)return false;this.src=this.src.split('-swap').join('');\" ";
		 return   $a;
	}

	function btsubmit($a=0)
	{
		if($a >= 1 ) $b = "Save";//$b = "Edit"; 
		else  $b = "Submit"; 
		return $b;
	}
	
	function act($int,$id)
	{
		if($int == $id) $a =  " class='act$id' ";
		return $a;
	}
	
	
	function Q2($select)
	{
		//echo $select;
		$q = array();
		$sql = mysql_query($select);
		if($sql) $_SESSION['numr'] = mysql_num_rows($sql);
		while($r=mysql_fetch_array($sql)) $q[] = $r;
		return $q;
	}
	function Q1($select)
	{
		// echo $select;
		$sql = mysql_query($select);
		if($sql) $_SESSION['numr'] = mysql_num_rows($sql);
		return $r=mysql_fetch_array($sql);
	}
	function Qinsert($db,$namedb,$valuearray)
	{	
		$q =  Qte($valuearray,$namedb);
		$a = " insert into $db (  ".$q[0]." ) values ( ".$q[1]." )  ";
		//echo $a;
		mysql_query($a) or die(mysql_error());
	
	}
	function Qupdate($db,$namedb,$valuearray,$where)
	{	
		$q =  Qte($valuearray,$namedb);
		$a = " update $db set ".$q[2]." where $where  ";
		mysql_query($a);
	
	}
	function Qdeltearray($db,$namedb,$valuearray,$priMkey)
	{	
		if(isset($valuearray[0]))
		{
		$q =  Qte($valuearray,$priMkey);
		$a = " DELETE FROM  $db WHERE   ".$q[3];	
		mysql_query($a);
		}
	
	}
	function Qn($select)
	{
		$rs =  mysql_query($select);
		while($data = mysql_fetch_array($rs)) $q[] = $data;
	
		return $q;
	}
	
	function Qte($valuearray,$namedb)
	{
		$i=0;
		$key2 = array_keys($valuearray);
		foreach($valuearray as $r)
		{
			
			if(($i) != 0) { $tag = ","; $tagand = " and ";  $tagor = " or "; }
			$newsql[0] .= $tag . $namedb.$key2[$i];
			$newsql[1] .= $tag . "'".$r."'";
			$newsql[2] .= $tag . $namedb.$key2[$i]."='".$r."'";
			$newsql[3] .= $tagor.$namedb."='".$r."'";
			$i++;
		}
		
		return $newsql;
	}
	
	function swap($a,$b)
	{
		
		if($_SESSION['lang'] == "th") $c=$a;
		else $c=$b;
	
		$c = abpath($c);
	
		return $c;
	}
	
	
	function Curlpic($newpath)
	{
		$dir = $newpath; // Directory name
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				$i = 0;
				while (($getfile = readdir($dh)) !== false) {
					$file[$i] = $getfile;
					$i++;
				}
				closedir($dh);
			}
			$filenum = count($file);
		}
		for($i=0;$i<sizeof($file);$i++) 
			{	
				if($i >= 2 && $file[$i] != "old") $file2[] = $file[$i];
			}
		
		return $file2;
	}
	
	
	function  dateb($a)
	{
		$a = explode("-",$a);
		$a = strtoupper(date("M Y", mktime(0, 0, 0, $a[1], $a[2], $a[0])));
		return  $a;
	}
	
	function selected($ft,$r,$how = 0)
	{
		$b = array("checked","selected");
		if(strstr($r,$ft."|")) $a = " ".$b[$how]." ";
		else $a = '';
		return $a;
	}
	
	function mysetting()
	{
		$r1 = Q2("select * from setting ");
		foreach($r1 as $r)
		{
			$stt[$r['stt_name']][0] = $r['stt_id'];
			$stt[$r['stt_name']][1] = $r['stt_name'];
			$stt[$r['stt_name']][2] = $r['stt_value'];
			$stt[$r['stt_name']][3] = $r['stt_order'];
		}
		return $stt;
	}
	
	
	function stylebgc($a)
	{
		if($a == "") $b = '';
		else $b = "background-color:#".str_replace("#","",$a).";";
		return $b;
	}
	
	function stylefontsize($a)
	{
		if($a == "") $b = '';
		else $b = "font-size:".str_replace(" ","",$a)."px;";
		return $b;
	}
	
	function stylefontc($a)
	{
		if($a == "") $b = '';
		else $b = "color:#".str_replace("#","",$a).";";
		return $b;
	
	}
	function fileExist($filename){
		//$filename = '/pathto/file.php'; 

		if (file_exists($filename)) { 
		//echo "The file $filename exists"; 
		return true;
		} else { 
		//echo "The file $filename does not exist"; 
		return false;
		} 	
	}
	function logosetting()
	{
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/logo.".$r)) return "imgs/logo.".$r;
		else if(file_exists("../imgs/logo.".$r)) return "../imgs/logo.".$r;
		}
		return '';
	}
	
	function logofootersetting()
	{
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/logofooter.".$r)) return "imgs/logofooter.".$r;
		else if(file_exists("../imgs/logofooter.".$r)) return "../imgs/logofooter.".$r;
		}
		return '';
	}
	function logofooterlinksetting(){
		$lfls = Q1	("select * from images where img_function = 'logofooter' ");
		return $lfls['img_link'];
	}
	function bgheader()
	{
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/bgheader.".$r)) return "imgs/bgheader.".$r;
		else if(file_exists("../imgs/bgheader.".$r)) return "../imgs/bgheader.".$r;
		}
		return '';
	}
	function bgheaderlink(){
		$lfls = Q1	("select * from images where img_function like '%bgheader%' ");
		return $lfls['img_link'];
	}
	function bgheaderrepeat(){
		$lfls = Q1	("select * from images where img_function like '%bgheader_repeat%' ");
		return sizeof($lfls);
	}
	function faviconsetting(){
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/favicon.".$r)) return "imgs/favicon.".$r;
		else if(file_exists("../imgs/favicon.".$r)) return "../imgs/favicon.".$r;
		}
		return '';
	}
	
	function lang1Pic()
	{
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/lang1pic.".$r)) return "imgs/lang1pic.".$r;
		else if(file_exists("../imgs/lang1pic.".$r)) return "../imgs/lang1pic.".$r;
		}
		return '';
	}
	
	function lang2Pic()
	{
		$filetype = array("gif","jpg","jpeg","png");
		foreach($filetype as $r)
		{
		if(file_exists("imgs/lang2pic.".$r)) return "imgs/lang2pic.".$r;
		else if(file_exists("../imgs/lang2pic.".$r)) return "../imgs/lang2pic.".$r;
		}
		return '';
	}
	
	function cleanText($type,$text)
	{
		if($type == PLAIN)
			 return addslashes(strip_tags($text));
		else
			return addslashes($text);
	}
	
	function showcattree($where,$style,$display,$ext){
		
		$show = "<div class = 'cat_tree' style = '".$style."' >";
			$chk = Q2("select * from category where    ".$where."  ");	
			foreach($chk as $r){
				
				$res = mysql_query("select c_parent from category where c_parent = '".$r['c_id']."' ");
				$cnt = mysql_num_rows($res);
				if($cnt > 0){
					$onclick = " onclick = \"
						$(this).parent().children('.cat_tree').toggle('fast');
						$(this).parent().children('.cat_tree').children('div').toggle('fast');
						
					\" ";
					//$ext = "style = 'font-weight:bold; font-size:18px'";
					$btnedit = "";
				}else{
					$onclick = "";
					$btnedit = " onclick = \"location.href = '?body=product&c_parent=".$r['c_id']."';\";  ";
				}
				$show .= "<div style = 'display:".$display.";cursor:pointer;'  >";
							if($onclick != ""){
								$show .= "<span ".$onclick." >+ </span>";	
							}else{
								$show .= "<span ".$onclick." >- </span>";
							}
							$show .= "<span  ><a ".$btnedit."  ".$ext.">".$r['c_name']." / ".$r['c_name2']."</a></span>";
							$show .= showcattree(" c_parent = ".$r['c_id']." " , $style , "block" , "");
				$show .= "</div>";
			}
		$show .= "</div>";
		return $show;
	}
	function showcattree2($where,$style,$display,$ext,$havesub){
		
		$show = "<div class = 'cat_tree' style = '".$style."' >";
			$chk = Q2("select * from category where    ".$where."  ");	
			foreach($chk as $r){
				
				$res = mysql_query("select c_parent from category where c_parent = '".$r['c_id']."' ");
				$cnt = mysql_num_rows($res);
				if($cnt > 0){
					$onclick = " onclick = \"
						$(this).parent().children('.cat_tree').toggle('fast');
						$(this).parent().children('.cat_tree').children('div').toggle('fast');
						
					\" ";
					//$ext = "style = 'font-weight:bold; font-size:18px'";
					$btnedit = "";
					
					if($havesub){
					$addsub = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?body=cate&c_parent=".$r['c_id']."&act=addsub\"><img src=\"images/mediamanager.png\" width=\"15\" height=\"15\" alt=\"Add sub category\" title=\"Add sub category\"  ".btswap()." border=\"0\" ></a>&nbsp;";
					}else {$addsub = "";}
					
					$btndel = "";
				}else{
					$onclick = "";
					$btnedit = " onclick = \"location.href = '?body=product&c_parent=".$r['c_id']."';\";  ";
					
					if($havesub){
					$addsub = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?body=cate&c_parent=".$r['c_id']."&act=addsub\"><img src=\"images/mediamanager.png\" width=\"15\" height=\"15\" alt=\"Add sub category\" title=\"Add sub category\"  ".btswap()." border=\"0\" ></a>&nbsp;";
					}else {$addsub = "";}
					
					$btndel = "<a href=\"?body=cate&c_id=".$r['c_id']."&delid=".$r['c_id']."\"  onclick=\"javascript:if(confirm('Are you sure you want to delete')){return true;} else{return false;}\" > <img src=\"images/cancel_f2.png\" alt=\"Delete\" title=\"Delete\"  width=\"15\" height=\"15\" border=\"0\" ".btswap()." > </a>";
					
				}
				$show .= "<div style = 'display:".$display.";cursor:pointer;'  >";
							if($onclick != ""){
								$show .= "<span ".$onclick." >+ </span>";	
							}else{
								$show .= "<span ".$onclick." >- </span>";
							}
							$show .= "<span  ><a ".$btnedit."  ".$ext.">".$r['c_name']." / ".$r['c_name2']."</a></span>";
							
							$show .= $addsub;
                            
							$show .= "&nbsp;<a href=\"?act=edit&body=cate&c_id=".$r['c_id']."\"><img src=\"images/addedit.png\" width=\"15\" height=\"15\" alt=\"Edit\" title=\"Edit\"  ".btswap()." border=\"0\" ></a>&nbsp;";
							
							$show .= $btndel;
							
							$show .= showcattree2(" c_parent = ".$r['c_id']." " , $style , "block" , "" , true);
				$show .= "</div>";
			}
		$show .= "</div>";
		return $show;
	}
	
	function showsitemap($c_id , $milestone = ""){
		$data = "";
		$item = Q1("select * from category where c_id = '".$c_id."' ");
		/*if($_SESSION[lng]==2){
			$c_name = $item[c_name2];
		}else{
			$c_name = $item[c_name];	
		}*/
		$c_name = $item[c_name2];
		$data .= $c_name."$milestone";
		$item2 =  Q2("select * from category where c_id = '".$item[c_parent]."' ");
		if(count($item2) == 0){// end of sitemap
			//$data .= "Home$milestone";
			
		}else{// continue of sitemap
			//echo count($item2);
			//print_r($item2);
			foreach($item2 as $i){
				$data .= ">>".showsitemap($i['c_id'] , $milestone );
			}
			
		}
		return $data;
	}
	
	function showsitemapId($c_id , $ids){
		$data = "";
		$item = Q1("select * from category where c_id = '".$c_id."' ");
		
		$id = $item[c_id];
		//echo ">>>$id<br/>";
		$ids[] = $id;
		$item2 =  Q2("select * from category where c_id = '".$item[c_parent]."' ");
		if(count($item2) == 0){// end of sitemap
			//$data .= "Home$milestone";
			
		}else{// continue of sitemap
			//echo count($item2);
			foreach($item2 as $i){
				$ids = showsitemapId($i['c_id'] , $ids );
			}
			
		}
		return $ids;
	}
	
	function reverseData($data , $oldnav ,$newnav){//("" , " >> " , " -> ")
		$data1;
		$arr = explode($oldnav,trim($data));
			$cnt = count($arr);
			//echo ">>>>$data<<<<";
			//print_r($arr);
			for($i = $cnt-1;$i >= 0;$i--){
				if($i > 0){
					$nav = $newnav;	
				}else{
					$nav = "";	
				}
				$data1 .= $arr[$i].$nav;
			}
		return $data1;
	}
	
	function reverseData2($data , $ids , $oldnav ,$newnav){//("" , " >> " , " -> ")
		$data1;
		$style = "
		<style type = 'text/css'>
			.smaplink{cursor:pointer;}
		</style>
		";
		echo $style;
		$arr = explode($oldnav,trim($data));
			$cnt = count($arr);
			//echo ">>>>$data<<<<";
			//print_r($arr);
			for($i = $cnt-1;$i >= 0;$i--){
				if($i > 0){
					$nav = $newnav;	
				}else{
					$nav = "";	
				}
				$onclick = " onclick = \"showdiv2('id','content','ajax_showplist.php?c_id=".$ids[$i]."');\"";
				($i == 0)?$curActive = "curActive":$curActive = "";
				$data1 .= "<span class = 'smaplink ".$curActive."' ".$onclick.">".$arr[$i]."</span>".$nav;
			}
		return $data1;
	}
	
	function getCatIdTree($c_id , $ids){
		$data = "";
		$item = Q1("select * from category where c_id = '".$c_id."' ");
		
		$id = $item[c_id];
		//echo ">>>$id<br/>";
		$ids[] = $id;
		$item2 =  Q2("select * from category where c_parent = '".$item[c_id]."' ");
		if(count($item2) == 0){// end of sitemap
			//$data .= "Home$milestone";
			
		}else{// continue of sitemap
			//echo count($item2);
			foreach($item2 as $i){
				$ids = getCatIdTree($i['c_id'] , $ids );
			}
			
		}
		return $ids;
	}
	
?>